package com.szxy.hotel.controller;

import com.szxy.hotel.entity.*;
import com.szxy.hotel.service.ClientService;
import com.szxy.hotel.service.GuestroomService;
import com.szxy.hotel.service.ReserveService;
import com.szxy.hotel.service.VipService;
import com.szxy.hotel.util.App;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GuestroomController {
    @Resource(name = "guestroomService")
    GuestroomService guestroomService;
    @Resource(name = "reserveService")
    ReserveService reserveService;
    @Resource(name = "clientService")
    ClientService clientService;
    @Resource(name = "vipService")
    VipService vipService;
    /**
     * 商品的详情信息
     */
    @RequestMapping("/Consume.do")
    @ResponseBody
    public Map<String,Object> Consume(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        Commodity commodity = guestroomService.selectConsume(id);
        if(commodity!=null){
            map.put("status", 0);
            map.put("message", "有数据");
            map.put("result", commodity);
        }else {
            map.put("status", 1);
            map.put("message", "没有数据");
            map.put("result", null);
        }
        return map;
    }
    /**
     * 消费商品
     */
    @RequestMapping("/buyCommodity.do")
    public String buyCommodity(Consume consume){
        if(consume.getConsumeRoomid()!=null&&consume.getConsumeNumber()!=null&&consume.getConsumeTime()!=null){
            if(consume.getConsumeNumber()>0){
                Room room = reserveService.findByroomid(consume.getConsumeRoomid());
                if(room!=null&&(room.getRoomState().equals("已预订")||room.getRoomState().equals("已入住"))){
                    String ts = consume.getConsumeTime();
                    String[] t = ts.split("-");
                    String y = t[0];
                    String m = t[1];
                    String d = t[2];
                    consume.setConsumeYear(y);
                    consume.setConsumeMonth(m);
                    consume.setConsumeDay(d);
                    consume.setConsumeUsername(room.getRoomName());
                    int result = guestroomService.insertConsume(consume);
                    if(result==1){
                        Client client = clientService.findByclientname(room.getRoomName());
                        Vip vip = vipService.selectByName(room.getRoomName());
                        if(vip.getVipDiscount().equals("不打折")){
                            Double money = client.getClientMoney();
                            money -= consume.getConsumeNumber()*consume.getConsumePrice();
                            clientService.updateClient(client.getClientName(),money);
                        }else {
                            Double dts = vip.getVipDisDouble();
//                            String ds = vip.getVipDiscount();
//                            char dt =  ds.charAt(0);
//                            String dis = new Character(dt).toString();
//                            Double discount = Double.parseDouble(dis);
//                            Double dc = discount/10;
                            Double money = client.getClientMoney();
                            money -= (consume.getConsumeNumber()*consume.getConsumePrice()*dts);
                            clientService.updateClient(client.getClientName(),money);
                        }
                    }
                }
            }
        }
        return "redirect:toGuestroom.do";
    }
    /**
     * 消费的商品列表
     */
    @RequestMapping("/guestroomConsumeList.do")
    @ResponseBody
    public Map<String,Object> guestroomConsumeList(int currentPage,String selectroomid){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Consume> pageBean = new PageBean<Consume>();
        int pageSize = 8;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        if(selectroomid==""||selectroomid==null){
            List<Consume> lists = guestroomService.findByPage(start, size);
            List<Consume> list = guestroomService.selectCount();
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else {
            List<Consume> lists = guestroomService.findByPage1(start, size,selectroomid);
            List<Consume> list = guestroomService.selectCount1(selectroomid);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }
        return map;
    }
    /**
     * 入住的房间列表
     */
    @RequestMapping("/guestroomList.do")
    @ResponseBody
    public Map<String,Object> guestroomList(int currentPage,String selectroomid){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Room> pageBean = new PageBean<Room>();
        String roomtype = "已入住";
        int pageSize = 8;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        if(selectroomid==""||selectroomid==null){
            List<Room> lists = reserveService.findByPage(start, size,roomtype);
            List<Room> list = reserveService.selectCount(roomtype);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }else {
            List<Room> lists = reserveService.findByPageSelectid(start, size,roomtype,selectroomid);
            List<Room> list = reserveService.selectCountSelectid(roomtype,selectroomid);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }
        return map;
    }
    /**
     * 续房
     */
    @RequestMapping("/continueGuestroom.do")
    @ResponseBody
    public Map<String,Object> continueGuestroom(String roomid,String roomName,String outRoomTime){
        Map<String, Object> map = new HashMap<String, Object>();
        if(outRoomTime==null){
            map.put("status", 1);
            map.put("message", "日期不能为空");
        }else {
            Room room = reserveService.findByroomid(roomid);
            String roomout = room.getRoomOut();
            String[] routs = roomout.split("-");
            int yout = Integer.parseInt(routs[0]);
            int mout = Integer.parseInt(routs[1]);
            int dout = Integer.parseInt(routs[2]);
            String[] outrooms = outRoomTime.split("-");
            int outy = Integer.parseInt(outrooms[0]);
            int outm = Integer.parseInt(outrooms[1]);
            int outd = Integer.parseInt(outrooms[2]);
            if(outy-yout==1){
                if(mout!=12||outm!=1){
                    map.put("status", 1);
                    map.put("message", "最多只能续7天");
                }else {
                    int dy = App.day(yout,mout);
                    if((dy-dout+outd)>7){
                        map.put("status", 1);
                        map.put("message", "最多只能续7天");
                    }else {
                        int roomtime = (dy-dout+outd);
                        roomtime += room.getRoomTime();
                        Client client = guestroomService.findByUsername(roomName);
                        Double money = client.getClientMoney();
                        if(money<(dy-dout+outd)*room.getRoomPrice()){
                            map.put("status", 1);
                            map.put("message", "余额不足请充值");
                        }else {
                            int result = guestroomService.updateDate(outRoomTime,roomtime,roomid);
                            if(result==1){
                                money -= (dy-dout+outd)*room.getRoomPrice();
                                guestroomService.updateClientMoney(money,roomName);
                                map.put("status", 0);
                                map.put("message", "续房成功");
                            }else {
                                map.put("status", 1);
                                map.put("message", "续房失败");
                            }
                        }
                    }
                }
            }else if(outy-yout==0){
                if(outm-mout==1){
                    int dy = App.day(yout,mout);
                    if((dy-dout+outd)>7){
                        map.put("status", 1);
                        map.put("message", "最多只能续7天");
                    }else {
                        int roomtime = (dy-dout+outd);
                        roomtime += room.getRoomTime();
                        Client client = guestroomService.findByUsername(roomName);
                        Double money = client.getClientMoney();
                        if(money<(dy-dout+outd)*room.getRoomPrice()){
                            map.put("status", 1);
                            map.put("message", "余额不足请充值");
                        }else {
                            int result = guestroomService.updateDate(outRoomTime,roomtime,roomid);
                            if(result==1){
                                money -= (dy-dout+outd)*room.getRoomPrice();
                                guestroomService.updateClientMoney(money,roomName);
                                map.put("status", 0);
                                map.put("message", "续房成功");
                            }else {
                                map.put("status", 1);
                                map.put("message", "续房失败");
                            }
                        }
                    }
                }else if(outm-mout==0){
                    if(outd-dout<=0){
                        map.put("status", 1);
                        map.put("message", "请正确填写日期");
                    }else if(outd-dout>7){
                        map.put("status", 1);
                        map.put("message", "最多只能续7天");
                    }else {
                        int roomtime = outd-dout;
                        roomtime += room.getRoomTime();
                        Client client = guestroomService.findByUsername(roomName);
                        Double money = client.getClientMoney();
                        if(money<(outd-dout)*room.getRoomPrice()){
                            map.put("status", 1);
                            map.put("message", "余额不足请充值");
                        }else {
                            int result = guestroomService.updateDate(outRoomTime,roomtime,roomid);
                            if(result==1){
                                money -= (outd-dout)*room.getRoomPrice();
                                guestroomService.updateClientMoney(money,roomName);
                                map.put("status", 0);
                                map.put("message", "续房成功");
                            }else {
                                map.put("status", 1);
                                map.put("message", "续房失败");
                            }
                        }
                    }
                }else if(outm-mout<0){
                    map.put("status", 1);
                    map.put("message", "请正确填写日期");
                }else {
                    map.put("status", 1);
                    map.put("message", "最多只能续7天");
                }
            }else {
                map.put("status", 1);
                map.put("message", "请正确填写日期");
            }
        }
        return map;
    }
}
